package com.study.netty.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author study
 * @version 1.0
 * @date 2020/9/28 16:30
 */
public class NewIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();

        socketChannel.connect(new InetSocketAddress("localhost", 7001));

        String fileName = "d:/导航栏.wmv";
        //得到文件 channel
        FileChannel fileChannel = new FileInputStream(fileName).getChannel();

        long startTime = System.currentTimeMillis();

        //linux 一个transferto就可以完成传输
        //window 一次调用只能发送8M文件，就需要分段发送，要注意传输时的位置。
        //底层使用了零拷贝
        long transferLong = fileChannel.transferTo(0, fileChannel.size(), socketChannel);
        System.out.println("发送中的字节数" + transferLong + " 总共耗时" + (System.currentTimeMillis() - startTime));

    }
}
